"use strict";
/* Controllers */
app.controller("ticketDealController", ['$scope', '$q', '$location', '$rootScope', '$routeParams', 'ticketDealService', function ($scope, $q, $location, $rootScope, $routeParams, ticketDealService) {

    var vm = $scope.vm = {};
    vm.p = {}; vm.d = {}; vm.result = {};
    vm.p.id = $routeParams.id; // 如果id!=0为更新
    vm.p.notes = [];
    vm.d.assignType = 'person';
    vm.d.responser = '';
    vm.d.newResponserShow = false;

    var errorFn = function (result) {
        alert(result);
    };

    $scope.dealTicket = function () {
        var vm = $scope.vm;
        var params = {
            id: vm.result.data.id,
            status: vm.result.data.status,
            responser: vm.result.data.responser,
            resolvegroupId: vm.result.data.resolvegroupId,
            ctiId: vm.result.data.ctiId
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("执行成功！");
            } else {
                alert(response.msg);
            }
        };
        ticketDealService.dealTicket(params, procFn, errorFn);
    };
    $scope.toResponser = function () {
        var vm = $scope.vm;
        if(!vm.d.responser || vm.d.responser.length < 2) {
            return;
        }
        vm.result.data.responser = vm.d.responser;


        $q.all([$scope.dealTicket()]).then(function () {
            vm.d.newResponserShow = false;
        });
    };


    $scope.addRemarks = function () {
        var vm = $scope.vm;
        var note = {
            datetime: hll2StdTime(hllCurrentTime()),
            user: '58oa',
            content: vm.p.note
        }
        vm.p.notes.push(note);

        vm.p.noteJson = JSON.stringify(vm.p.notes);
        var params = {
            id: vm.result.data.id,
            remarks: vm.p.noteJson
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                console.log("执行成功！");
            } else {
                alert(response.msg);
            }
        };
        ticketDealService.addRemarks(params, procFn, errorFn);
    };

    $scope.getTicket = function () {
        var vm = $scope.vm;
        var params = {
            id: vm.p.id
        };
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.result.data = res.data;

                vm.p.type = typeTxt(vm.result.data.type);
                vm.p.category = categoryTxt(vm.result.data.category);
                vm.p.severity = severityTxt(vm.result.data.severity);

                if(vm.result.data.remarks!=null && vm.result.data.remarks.length > 4) {
                    try {
                        vm.p.remarks = JSON.parse(vm.result.data.remarks);
                    }catch(e) {
                        vm.p.remarks = [{
                                content: vm.result.data.remarks
                            }
                        ];
                    }
                }
                if(vm.result.data.resolvegroupId!=null && vm.result.data.resolvegroupId.length>1) {
                    vm.d.assignType = 'resolvegroup';
                }
            } else {
                alert(res.message);
            }
        };
        ticketDealService.getTicket(params, procFn, errorFn);
    };

    $scope.initializeController = function() {
        if(vm.p.id!=0) {
            console.log(vm.p.id);
            $scope.getTicket();
        }
    };
    $scope.initializeController();
}]);


/* Services */
app.service('ticketDealService', ['ajaxService', function (ajaxService) {

    this.getTicket = function (tickets, successFunction, errorFunction) {
        ajaxService.AjaxPost(tickets, "/star/api/onduty-app-service/v1/ticket/getTicket.do", successFunction, errorFunction);
    };

    this.dealTicket = function (tickets, successFunction, errorFunction) {
        ajaxService.AjaxPost(tickets, "/star/api/onduty-app-service/v1/ticket/dealTicket.do", successFunction, errorFunction);
    };

    this.addRemarks = function (tickets, successFunction, errorFunction) {
        ajaxService.AjaxPost(tickets, "/star/api/onduty-app-service/v1/ticket/addRemarks.do", successFunction, errorFunction);
    };
}]);

function typeTxt(value) {
    if (value == null) return '-';
    var ret = '';
    value = value.toLowerCase();
    if (value == 'data') {
        ret = '数据治理';
    } else if (value == 'bug') {
        ret = '系统异常';
    } else if (value == 'service') {
        ret = '服务支持';
    } else if (value == 'account') {
        ret = '权限申请';
    } else if (value == 'host') {
        ret = '主机申请';
    } else if (value == 'policy') {
        ret = '规范政策';
    } else {
        ret = '未定义'+value;
    }
    return ret;
}


function categoryTxt(value) {
    if (value == null) return '-';
    return value;
}

function severityTxt(value) {
    if (value == null) return '-';
    var ret = '';
    value = value.toLowerCase();
    if (value == 'critical') {
        ret = '严重的';
    } else if (value == 'urgent') {
        ret = '紧急的';
    } else if (value == 'high') {
        ret = '高的';
    } else if (value == 'normal') {
        ret = '一般的';
    } else if (value == 'low') {
        ret = '低的';
    } else {
        ret = '未知:' + value;
    }
    return ret;
}
